package com.fx.ruankao.exception;

import com.fx.ruankao.utils.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理类
 * 统一处理系统中抛出的所有异常
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有未捕获的异常
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e, HttpServletRequest request) {
        // 记录异常信息
        e.printStackTrace();
        // 返回统一的错误响应
        return Result.error("系统异常：" + e.getMessage());
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public Result handleBusinessException(BusinessException e, HttpServletRequest request) {
        // 业务异常通常不需要打印堆栈，只返回错误信息即可
        return Result.error(e.getMessage());
    }

    /**
     * 处理参数验证异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public Result handleIllegalArgumentException(IllegalArgumentException e, HttpServletRequest request) {
        // 参数验证异常返回具体错误信息
        return Result.error("参数错误：" + e.getMessage());
    }
}